package com.sunshine.litemusic.activity.main;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.sunshine.litemusic.R;
import com.sunshine.litemusic.base.BaseActivity;
import com.sunshine.litemusic.fragment.music.MusicFragment;
import com.sunshine.litemusic.fragment.search.SearchModel;

import java.util.ArrayList;

/**
 * @author sunshine
 * @date 2020/05/28
 * 主活动，闪屏过后展示给用户的第一屏
 */
public class MainActivity extends BaseActivity {

    private ArrayList<Fragment> fragmentArrayList;  //fragment列表，用于fragment之间传递数据
    private BottomNavigationView bottomNavigationView;
    private ViewPager viewPager;
    private AppCompatTextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    /**
     * 基础view功能
     */
    private void initView() {
        //基础标题设置
        setNoTitle();
        Toolbar mainToolbar = findViewById(R.id.toolbar_main);
        setSupportActionBar(mainToolbar);
        title = findViewById(R.id.textView_title);

        //一些控件的初始化
        bottomNavigationView = findViewById(R.id.bnv);
        viewPager = findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(2);
        MainAdapter mainAdapter = new MainAdapter(getSupportFragmentManager());
        fragmentArrayList = mainAdapter.getFragmentList();
        viewPager.setAdapter(mainAdapter);

        //设置默认展示的界面，这里是播放器界面
        viewPager.setCurrentItem(1);
        bottomNavigationView.getMenu().getItem(1).setChecked(true);
        setMyTitle(1);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.recommend:
                        viewPager.setCurrentItem(0);
                        setMyTitle(0);
                        break;
                    case R.id.music:
                        viewPager.setCurrentItem(1);
                        setMyTitle(1);
                        break;
                    case R.id.search:
                        viewPager.setCurrentItem(2);
                        setMyTitle(2);
                        break;
                }
                return true;
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                bottomNavigationView.getMenu().getItem(position).setChecked(true);
                setMyTitle(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 搜索的歌曲播放
     * @see com.sunshine.litemusic.fragment.music.MusicFragment
     * @param searchBeanArrayList 要播放的歌曲列表
     * @param position 当前要播放的位序
     */
    public void searchToPlay(ArrayList<SearchModel.SearchBean> searchBeanArrayList, int position){
        ((MusicFragment) fragmentArrayList.get(1)).searchPlay(searchBeanArrayList, position);
        bottomNavigationView.getMenu().getItem(1).setChecked(true);
        viewPager.setCurrentItem(1);
        setMyTitle(1);
    }

    /**
     * 设置标题文字
     * @param position 页面id
     */
    private void setMyTitle(int position){
        switch (position){
            case 0: title.setText("推荐"); break;
            case 1: title.setText("播放器"); break;
            case 2: title.setText("搜索"); break;
        }
    }
}
